/**
 * 起重机械标准配置craneStandard.js
 * 版本号： v1.0.0
 */
$(function () {

    //设置输入框的最大限制长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[0,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });

    var lastChecked = null;
    $("#craneStandard_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/craneStandard/craneStandardList.do',
        idField: 'csId',
        frozenColumns: [[
            {field: 'csId', checkbox: true},
            {
                field: 'csType',
                title: '起重器械类型',
                width: 120,
                fixed: true,
                formatter:function(val){
                    if(val) {
                        return val.propertyName;
                    }
                }},
            {field: 'csVer', title: '起重器械型号|设备型号', width: 150, fixed: true},
            {
                field: 'basicType',
                title: '塔机结构类型',
                width: 100,
                fixed: true,
                formatter:function(val){
                    if(val) {
                        return val.propertyName;
                    }
                }
            }
        ]],
        columns: [
            [
                {field: 'capacity', title: '塔机|升降机标准额定起重量(t)', width: 180,  fixed: true},
                {field: 'torque', title: '塔机额定起重力矩(Kn.m)', width: 150,  fixed: true},
                {field: 'maxRange', title: '塔机最大工作幅度(m)', width: 150,  fixed: true},
                {field: 'maxWindSpeed', title: '塔机承受最大风速(m/s)', width: 150,  fixed: true},
                {field: 'liftHeight', title: '塔机|升降机最大起升高度(m)', width: 180,  fixed: true},
                {field: 'slewingSpeed', title: '塔机回转速度(r/min)', width: 150,  fixed: true},
                {field: 'forearmLen', title: '塔机标准前臂长(m)', width: 130,  fixed: true},
                {field: 'upperArm', title: '塔机标准后臂长(m)', width: 130,  fixed: true},
                {field: 'height', title: '塔机标准塔帽高度(m)', width: 130,  fixed: true},
                {field: 'balanWeiHei', title: '塔机标准配重高度(m)', width: 130,  fixed: true},
                {field: 'frontAxle', title: '塔机前桥桥高(m)', width: 130,  fixed: true},
                {field: 'ropeDroopDist', title: '塔机钢绳下垂距离(m)', width: 130,  fixed: true},
                {field: 'frontAxleWidth', title: '塔机前桥宽(m)', width: 100,  fixed: true},
                {field: 'rearAxleWidth', title: '塔机后桥宽(m)', width: 100,  fixed: true},
                {field: 'coordinateX', title: '塔机坐标X', width: 100,  fixed: true},
                {field: 'coordinateY', title: '塔机坐标Y', width: 100,  fixed: true},
                {field: 'capaAtMaxRange', title: '塔机最大幅度额定起重量(t)', width: 180,  fixed: true},
                {field: 'liftSpeed', title: '塔机|升降机起升速度(m/min)', width: 180,  fixed: true},
                {field: 'derrickingSpeed', title: '塔机变幅速度(m/min)', width: 150,  fixed: true},
                {field: 'deadWeight', title: '塔机|升降机自重(t)', width: 150,  fixed: true},
                {field: 'balanceWeight', title: '塔机平衡重量(t)', width: 120,  fixed: true},
                {field: 'createBy', title: '创建者', width: 120,  fixed: true},
                {field: 'createDate', title: '创建时间', width: 150,  fixed: true,formatter:function(val){
                    if(val){
                        return formatDate(val);
                    }
                    }},
                {field: 'updateBy', title: '更新者', width: 120,  fixed: true},
                {field: 'updateDate', title: '更新时间', width: 180,  fixed: true,formatter:function(val){
                        if(val){
                            return formatDate(val);
                        }
                    }},
                {field: 'remark', title: '备注', width: 100,  fixed: true}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#craneStandard_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#craneStandard_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#craneStandard_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#craneStandard_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 删除起重机械标准配置
 */
function craneStandard_del() {
    var row = $("#craneStandard_dataList").datagrid("getSelected");
    var checked = $("#craneStandard_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择删除起重机械标准配置", timeout: 3000, showType: "slide"});
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.csVer + ' 起重机械标准配置吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/craneStandard/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#craneStandard_dataList").datagrid("reload", {
                        csVer:$("#csVer_").val(),
                        csType:$("#csType_").combobox("getValue"),
                        basicType:$("#basicType_").combobox("getValue")
                    });
                }
            })
        }
    });
}


function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            window.location.href = CONTEXT_PATH + "/craneStandard/toCraneStandard.do";
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            craneStandard_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            craneStandard_edit();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            craneStandard_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            craneStandard_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查询起重机械标准配置
 */
function craneStandard_findList() {
    $("#craneStandard_dataList").datagrid("reload",{
        csVer:$("#csVer_").val(),
        csType:$("#csType_").combobox("getValue"),
        basicType:$("#basicType_").combobox("getValue")
    });
    //清楚选中项
    $("#craneStandard_dataList").datagrid("clearSelections");
}

/**
 * 修改起重机标准跳转
 * @returns {boolean}
 */
function craneStandard_edit() {
    var row = $("#craneStandard_dataList").datagrid("getSelected");
    var checked = $("#craneStandard_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择删除起重机械标准配置", timeout: 3000, showType: "slide"});
        return;
    }
    window.location.href = CONTEXT_PATH + "/craneStandard/toCraneStandard.do?csId="+row.csId;
}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看项目详情
 */
function craneStandard_view(iconCls){

    var row = $("#craneStandard_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的起重机械标准详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(qzjxbzxq.id, qzjxbzxq.url + row.csId, iconCls, qzjxbzxq.closable, qzjxbzxq.title);
}